<template>
  <div class="news-page">
    <div class="content-wrapper">
      <h1 class="page-title">新闻资讯</h1>
      
      <!-- 新闻列表 -->
      <el-row :gutter="20">
        <el-col :span="8" v-for="item in newsList" :key="item.id">
          <el-card class="news-card" shadow="hover" @click="handleNewsClick(item)">
            <div class="news-image-container">
              <img 
                v-if="item.image" 
                :src="item.image" 
                class="news-image"
                @error="handleImageError"
              >
              <div v-else class="news-image-placeholder">
                <el-icon :size="40"><Picture /></el-icon>
              </div>
            </div>
            <div class="news-info">
              <h3>{{ item.title }}</h3>
              <p class="summary">{{ item.summary }}</p>
              <div class="news-meta">
                <span class="time">{{ formatDate(item.createTime) }}</span>
                <span class="author">{{ item.author }}</span>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[9, 18, 36]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import request from '@/utils/request'
import { formatDate } from '@/utils/format'

const router = useRouter()
const newsList = ref([])
const currentPage = ref(1)
const pageSize = ref(9)
const total = ref(0)

const loadNews = async () => {
  try {
    const res = await request.get('/api/news/list', {
      params: {
        page: currentPage.value,
        size: pageSize.value
      }
    })
    if (res.code === 200) {
      newsList.value = res.data.records
      total.value = res.data.total
    }
  } catch (error) {
    console.error('加载新闻失败:', error)
  }
}

const handleNewsClick = (news) => {
  router.push(`/front/news/${news.id}`)
}

const handleSizeChange = (val) => {
  pageSize.value = val
  loadNews()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  loadNews()
}

const handleImageError = (e) => {
  const target = e.target
  if (target) {
    target.style.display = 'none'
    target.parentElement.innerHTML = `
      <div class="news-image-placeholder">
        <el-icon :size="40"><Picture /></el-icon>
      </div>
    `
  }
}

onMounted(() => {
  loadNews()
})
</script>

<style lang="scss" scoped>
.news-page {
  .content-wrapper {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
  }

  .page-title {
    font-size: 28px;
    color: #303133;
    margin-bottom: 30px;
    text-align: center;
  }

  .news-card {
    margin-bottom: 20px;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
    }

    .news-image-container {
      width: 100%;
      height: 200px;
      overflow: hidden;
      border-radius: 4px;
      margin-bottom: 12px;
      background-color: #f5f7fa;
      display: flex;
      align-items: center;
      justify-content: center;

      .news-image {
        width: 100%;
        height: 100%;
        object-fit: cover;
      }

      .news-image-placeholder {
        color: #909399;
      }
    }

    .news-info {
      padding: 15px;

      h3 {
        margin: 0 0 10px;
        font-size: 18px;
        color: #303133;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .summary {
        color: #606266;
        line-height: 1.6;
        height: 48px;
        overflow: hidden;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        margin-bottom: 10px;
      }

      .news-meta {
        display: flex;
        justify-content: space-between;
        color: #909399;
        font-size: 14px;
      }
    }
  }

  .pagination-container {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style> 